<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Cache;
use Mockery\Exception;

class Book extends Model
{
    protected $guarded = [];
    public $timestamps = false;

    //只查询上架的图书
    public function scopeState($query)
    {
        return $query->where('state', '=', 1);
    }

    //只查询未删除的图书
    public function scopeUndeleted($query)
    {
        return $query->where('deleted', 0);
    }

    //只查询有库存的图书
    public function scopeStock($query)
    {
        return $query->where('total_stock', '>', 0);
    }


    public function categories()
    {
        return $this->belongsToMany(Category::class, 'book_category_relations','book_id','category_id');
    }

    public function homepage_recommend_modules()
    {
        return $this->belongsToMany(HomepageRecommendModule::class, 'book_homepage_recommend_module_relations','book_id','homepage_recommend_module_id')->select('homepage_recommend_modules.id','homepage_recommend_modules.title');
    }

    public function parent_catgeory()
    {
        return $this->belongsTo(Category::class,'category_pid');
    }

    public function carts()
    {
        return $this->hasMany(Cart::class);
    }

    public function comments()
    {
        return $this->hasMany(BookComment::class);
    }

    public function addCommentNum($num = 1)
    {
        $this->increment('comment_num',$num);
    }

    public function decreaseCommentNum($num = 1)
    {
        return $this->newQuery()->where('id', $this->id)->where('comment_num', '>=', $num)->decrement('comment_num', $num);
    }

    public function book_lists()
    {
        return $this->belongsToMany(BookList::class,"book_list_book_relations","book_id","book_list_id");
    }

    public function getBookInfo($id)
    {
        $result = [];
        $book = $this->newQuery()->where('id',$id)->first();

        if(empty($book) || $book->deleted || $book->state == 2)
        {
            return $result;
        }

        $result = [
            'id'                    => $book->id,
            'title'                 => $book->title,
            'image'                 => $book->image,
            'author'                => $book->author,
            'old_price'             => $book->old_price,
            'category_pid'          => $book->category_pid,
            'category_ids'          => $book->category_ids,
            'sub_title'             => $book->sub_title,
            'score'                 => $book->score,
            'author_intro'          => $book->author_intro,
            'publish_house'         => $book->publish_house,
            'publish_time'          => $book->publish_time,
            'publish_num'           => $book->publish_num,
            'publish_translator'    => $book->publish_translator,
            'publish_package'       => $book->publish_package,
            'publish_paper'         => $book->publish_paper,
            'publish_pages'         => $book->publish_pages,
            'publish_open'          => $book->publish_open,
            'isbn'                  => $book->isbn,
            'desc'                  => $book->desc,
            'catalog'               => $book->catalog,
            'total_stock'           => $book->total_stock,
            'new_book_price'        => $book->new_book_price,
            'new_book_stock'        => $book->new_book_stock,
            'good_book_price'       => $book->good_book_price,
            'good_book_stock'       => $book->good_book_stock,
            'medium_book_price'     => $book->medium_book_price,
            'medium_book_stock'     => $book->medium_book_stock,
            'comment_num'           => $book->comment_num,
            'new_book_discount'     => $book->new_book_discount,
            'good_book_discount'    => $book->good_book_discount,
            'medium_book_discount'  => $book->medium_book_discount,
            'is_recycle'            => $book->is_recycle,
            'recyle_price'          => $book->recyle_price,
            'medium_book_price2'    => $book->medium_book_price2,
            'medium_book_stock2'    => $book->medium_book_stock2,
            'medium_book_discount2' => $book->medium_book_discount2,
            'medium_book_price3'    => $book->medium_book_price3,
            'medium_book_stock3'    => $book->medium_book_stock3,
            'medium_book_discount3' => $book->medium_book_discount3,
            'medium_book_price4'    => $book->medium_book_price4,
            'medium_book_stock4'    => $book->medium_book_stock4,
            'medium_book_discount4' => $book->medium_book_discount4,
            'medium_book_price5'    => $book->medium_book_price5,
            'medium_book_stock5'    => $book->medium_book_stock5,
            'medium_book_discount5' => $book->medium_book_discount5,
            'medium_book_price6'    => $book->medium_book_price6,
            'medium_book_stock6'    => $book->medium_book_stock6,
            'medium_book_discount6' => $book->medium_book_discount6,
        ];
        return $result;

        //Cache::forget('book-shop:books:info:book-id:'.$id);die;
        /*return Cache::remember('book-shop:books:info:book-id:'.$id, 24*60, function() use ($id){
            return $this->bookInfo($id);
        });*/
    }

    public function decreaseStock($amount)
    {
        if ($amount < 0) {
            throw new \Exception('减库存不可小于0');
        }
        return $this->newQuery()->where('id', $this->id)->where('total_stock', '>=', $amount)->decrement('total_stock', $amount);
    }

    public function addStock($amount)
    {
        if ($amount < 0) {
            throw new \Exception('加库存不可小于0');
        }
        $this->increment('total_stock', $amount);
    }
}
